package com.yanqu.road.server.cmd.activity.xingyoushanshui.cross;

import com.yanqu.road.entity.activity.youxing.XingYouShanShuiUserData;
import com.yanqu.road.entity.activity.youxing.config.XingYouConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shanshui.XingYouShanShuiModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.shanshui.XingYouShanShuiMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_XING_YOU_SHAN_SHUI_CONFIRM_EVENT_FROM_CROSS,desc = "二次确认事件-跨服回包")
public class ShanShuiConfirmEventFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        XingYouShanShuiProto.ShanShuiConfirmEventRsp rsp = XingYouShanShuiProto.ShanShuiConfirmEventRsp.parseFrom(packet.getBytes());
        XingYouConfig config = XingYouShanShuiMgr.getConfig();
        if(rsp.getRet() == 0){
            if(!StringUtils.isNullOrEmpty(rsp.getAward())) {
                Property reward = PropertyHelper.parseStringToProperty(rsp.getAward());
                //银票不入库
                reward.removeProperty(config.getSHANSHUI_SILVER_COIN_ID());
                if(rsp.getDrift()){
                    reward.removeProperty(config.getSHANSHUI_GOLD_COIN_ID());
                }
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.XingYouShanShuiActivity, eLogMoneyType.XingYouShanShuiSecondConfirmReward);
            }

            if(rsp.hasRound()) {
                int round = rsp.getRound();
                XingYouShanShuiUserData userData = player.getModule(XingYouShanShuiModule.class).getUserData();
                if (userData != null) {
                    userData.setRound(round);
                    player.notifyListener(eGamePlayerEventType.XingYouShanShuiUserRoundCount.getValue(), round);
                }
            }
        }

        player.sendPacket(Protocol.U_XING_YOU_SHAN_SHUI_CONFIRM_EVENT, rsp.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
